//
//  UIImageView+CircleImage.m
//  TravelGuideMdd
//
//  Created by 陈曦 on 13-10-24.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#import "UIImage+CircleImage.h"

@implementation UIImage(CircleImage)

- (UIImage*)circleImage
{
    CGFloat width = MIN(self.size.width, self.size.height);
    CGRect rect = CGRectMake((self.size.width - width) / 2, (self.size.height - width)/2, width, width);
    
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(path, nil, rect);
    CGContextAddPath(context, path);
    CGContextClip(context);
    CGPathRelease(path);
    
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0.0, rect.size.height);
    CGContextScaleCTM(context, 1, -1.0);
    CGContextDrawImage(context, rect, self.CGImage);
    CGContextRestoreGState(context);
    UIImage *newimage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimage;
}

@end
